﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CreateWinForm
{
    public delegate void ClickHandle(object sender);
    public partial class CreateForm : Form
    {
        public Orders.Order NewOrder = new Orders.Order();
        public List<Orders.Goods> GoodsList= new List<Orders.Goods>();
        public event ClickHandle SubmitClick;

        public CreateForm()
        {
            InitializeComponent();

            GoodsList.Add(new Orders.Goods("apple", 3));
            GoodsList.Add(new Orders.Goods("pear", 5));
            GoodsList.Add(new Orders.Goods("peach", 8));
           
            bindingSource1.DataSource = NewOrder.OrderItems;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            GoodsUnitPrice1Txt.Text = GoodsList[comboBox1.SelectedIndex].UnitPrice.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Orders.OrderItem newOrderItem = new Orders.OrderItem();
            newOrderItem.OrderGoods = GoodsList[comboBox1.SelectedIndex];
            newOrderItem.Quantity = Int32.Parse(textBox2.Text);
            newOrderItem.Price = newOrderItem.OrderGoods.UnitPrice * newOrderItem.Quantity;

            label8.Text = newOrderItem.Price.ToString();

            NewOrder.OrderItems.Add(newOrderItem);

            NewOrder.TotalPrice += newOrderItem.Price;
            label10.Text = NewOrder.TotalPrice.ToString();
            bindingSource1.ResetBindings(false);
        }

        private void button3_Click(object sender, EventArgs e)
        {
           
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
            NewOrder.Customer = new Orders.Customer(textBox1.Text);
            NewOrder.OrderNum = Int32.Parse(textBox3.Text);
            SubmitClick(this);
             this.Close();
        }
    }
}
